package com.zrs.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrs.gmall.common.result.Result;
import com.zrs.gmall.product.model.SkuInfo;
import com.zrs.gmall.product.service.SkuManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhaoRuiSheng
 * @create 2023/2/24 14:59
 */

@Api(tags = "商品SKU属性")
@RestController
@RequestMapping("/admin/product")
public class SkuManageController {
    @Autowired
    private SkuManageService skuManageService;


    /**
     * 保存商品SKU信息
     * @param skuInfo
     * @return
     */
    @ApiOperation(value = "保存商品SKU信息")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 查询商品SKU列表
     * @param page 页面
     * @param limit 页大小
     * @param skuInfo 查询条件
     * @return
     */
    @ApiOperation(value = "查询商品SKU列表")
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SkuInfo skuInfo){
        //  构建一个Page对象
        Page<SkuInfo> skuInfoPage = new Page<>(page,limit);
        //  调用服务层方法！
        IPage iPage = skuManageService.getSkuInfoPage(skuInfoPage,skuInfo);
        return Result.ok(iPage);
    }


    /**
     * 商品SKU上架
     * @param skuId
     * @return
     */
    @ApiOperation(value = "商品SKU上架")
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        skuManageService.onSale(skuId);
        return Result.ok();
    }


    /**
     * 商品SKU下架
     * @param skuId
     * @return
     */
    @ApiOperation(value = "商品SKU下架")
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        skuManageService.cancelSale(skuId);
        return Result.ok();
    }
}
